﻿$(document).ready(function () {
    bindAmountSlider("num");
    $("input[type='text']").numeral(true);
    $("input[type='submit']").bind("click", function () {
        $("#hd_betValue").val(getBetOrderJson());
    });

    $(".region").bind("change", function () {
        var chk = $(this);

        var companyArr = new Array();
        for (var i = 1; i <= 10; i++) {
            $(".company" + i).each(function (i, obj) {
                obj = $(obj);
                if (obj.attr("region") == chk.attr("name")) {
                    companyArr.push(obj.parent("span"));
                }
            });
        }

        var ischeck = isCheck(chk);
        $(companyArr).each(function (i, obj) {
            $(this).find(":checkbox").attr("checked", "");
            if (ischeck)
                $(obj).show();
            else
                $(obj).hide();
        });
    });

    $(".allChecked").bind("change", function () {
        var chk = $(this);
        var companyId = chk.attr("id").split("_")[1];
        var companyArr = new Array();
        for (var i = 1; i <= 10; i++) {
            $(".company" + i).each(function (i, obj) {
                obj = $(obj);
                if (obj.attr("company") == companyId) {
                    companyArr.push(obj.parent("span"));
                }
            });
        }

        var ischeck = isCheck(chk);
        $(companyArr).each(function (i, obj) {
            if (ischeck)
                $(this).find(":checkbox").attr("checked", true);
            else
                $(this).find(":checkbox").attr("checked", "");
        });
    });

    $("#betTable input[type='text']").bind("keyup", calcAmount);
    $("#betTable input[type='checkbox']").bind("change", calcAmount);
});

var calcAmount = function () {
    try {
        CalcBetAmount();
    }
    catch (e) {
        showMessage("Error", e);
    }
}
//---------------------------------------------------------------------------------------------------
var getNotNullNums = function () {
    allTxtBox = $("#betTable input[type='text']");        //获取所有文本框
    allChk = $("#betTable input[type='checkbox']");       //获取所有复选框
    var numTextbox = new Array();                       //存放要下注的号码
    //找到填写号码的文本框
    $(".num").each(function (i, obj) {
        obj = $(obj);
        var numValue = obj.val();
        if (!isNullOrEmpty(numValue))
            numTextbox.push(obj);
    });
    return numTextbox;
}

var getBetOrderJson = function () {
    var numTextbox = getNotNullNums();
    var betArr = new Array();
    numTextbox = $(numTextbox);
    numTextbox.each(function (i, obj) {
        var wagerStr = getWagerJson($(obj).attr("rowid"));
        var num = obj.val();
        var company = getCheckCompany(obj.attr("rowid"));
        if (company.length == 0) return true;
        var numArr = $(getBetNums(num));
        numArr.each(function (i, number) {
            var sb = new Array();
            sb.push("{\"CompanyList\":[" + company.join() + "]");
            sb.push("\"Num\":" + number);
            sb.push("\"WargerList\":" + wagerStr + "}");
            betArr.push(sb.join());
        });
    });
    var json = betArr.join();
    if (!isNullOrEmpty(json))
        json = "[" + json + "]";
    return json;
}
//获取要下注的号码
var getBetNums = function (numStr) {
    var numArr = new Array();
    if (!isNaN(numStr))
        numArr.push(numStr);
    else if (/^\d{2}-\d{2}$/.exec(numStr)) {    //2D连数；如11-49
        $(getNums(numStr)).each(function (i, x) { numArr.push(x); });
    }
    else if (/^\d{3}-\d{3}$/.exec(numStr)) {    //3D
        $(getNums(numStr)).each(function (i, x) { numArr.push(x); });
    }
    else if (/^\d{4}-\d{4}$/.exec(numStr)) {    //4D
        $(getNums(numStr)).each(function (i, x) { numArr.push(x); });
    }
    return numArr;
}
//得到连数中所有的有效的数字
var getNums = function (numStr) {
    var numArr = new Array();
    var nums = numStr.split("-");
    var numA = parseInt(nums[0]);
    var numB = parseInt(nums[1]);
    if (numA <= numB) {
        var i;
        for (i = numA; i <= numB; i++)
            numArr.push(i);
    }
    return numArr;
}
//从所有选项中（公司）找到指定行选中的公司
var getCheckCompany = function (rowId) {
    var checkCompanyArr = new Array();
    $(".company" + rowId).each(function (i, obj) {
        obj = $(obj);
        if (isCheck(obj)) {
            var company = obj.attr("company");
            if (!isNullOrEmpty(company))
                checkCompanyArr.push(obj.attr("company"));
        }
    });
    return checkCompanyArr;
}
//获取玩法下注额
var getWagerJson = function (rowId) {
    var txtBoxArr = new Array();        //存放当前Row所有投注金额
    $(".wager" + rowId).each(function (i, obj) {
        obj = $(obj);
        if (!isNullOrEmpty(obj.val()))
            txtBoxArr.push(obj);
    });

    var wagerArr = new Array();
    wagerArr.push("[");
    var isFirst = true;
    $(txtBoxArr).each(function (i, obj) {
        if (isFirst)
            isFirst = false;
        else
            wagerArr.push(",");
        var chk = $("#" + obj.attr("id") + "_ck");
        var isSelected = isCheck(chk);
        var sb = new Array();
        var gamePlayWayInfo = obj.attr("id").split("_");
        sb.push("{\"GamePlayTypeId\":" + gamePlayWayInfo[2]);
        sb.push("\"IsFullPermutation\":" + isSelected);
        sb.push("\"Wager\":" + obj.val() + "}");
        wagerArr.push(sb.join());
    });
    wagerArr.push("]");
    return wagerArr.join("");
}
//从指定行中找到特定游戏玩法且选中的checkbox
var findCheckBox = function (gamePlayWay, rowId) {
    var allChk = $("#betTable input[type='checkbox']");
    var chk;
    allChk.each(function (i, obj) {
        obj = $(obj);
        if (obj.attr("gameplayway") == gamePlayWay && obj.attr("rowid") == rowId) {
            chk = obj;
            return false;
        }
    });
    return chk;
}
//-----------------------Begin  金额计算------------------------------------------
//计算下注金额
var CalcBetAmount = function () {
    var numArr = $(getNotNullNums());   //获取不为空的号码textbox
    InitCompanyNumLen();                //初始化公司支持号码长度
    numArr.each(function (i, obj) {
        obj = $(obj);
        var nums = getBetNums(obj.val());
        var totalAmount = 0;
        var rowId = obj.attr("rowid");
        $(nums).each(function (t, x) {
            totalAmount += CalcRowAmount(x, rowId);
        });
        $("#amount" + rowId).html(totalAmount);
    });
}
var CalcRowAmount = function (num, rowId) {
    var wagerArr = new Array();         //所有本行下注额
    $(".wager" + rowId).each(function (i, obj) {
        obj = $(obj);
        if (!isNullOrEmpty(obj.val()))
            wagerArr.push(obj);
    });

    var companys = getCheckCompany(rowId);
    var totalAmount = 0;
    $(companys).each(function (t, x) {
        var comNumLen = findCompanyNumLen(x);
        $(wagerArr).each(function (i, obj) {
            var chk = $("#" + obj.attr("id") + "_ck");
            var isSelected = isCheck(chk);
            var gameplaywayInfo = obj.attr("id").split("_");
            var number = getParseNum(gameplaywayInfo[0], num);
            var numLen = getPlayWayNumLen(gameplaywayInfo[1], number, comNumLen);
            var amount = obj.val() * numLen * (isSelected ? countPermutation(number) : 1);
            totalAmount += amount;
        });

    });
    return totalAmount;
}

//根据公司Id获取号码长度
var findCompanyNumLen = function (companyId) {
    var numLen;
    $(CompanyNumLen).each(function (i, obj) {
        if (obj.CompanyId == companyId) {
            numLen = obj;
            return false;
        }
    });
    return numLen;
}
var getParseNum = function (gameType, num) {
    var number = num.toString();
    if (gameType == "2D") {
        if (number.length > 2) {
            num = number.substring(number.length - 2);
        }
        else if (number.length < 2)
            throw new Error("Error Num！");
    }
    else if (gameType == "3D") {
        if (number.length > 3) {
            num = number.substring(number.length - 3);
        }
        else if (number.length < 3)
            throw new Error("Error Num！");
    }
    else if (gameType == "4D") {
        if (number.length > 4)
            num = number.substring(number.length - 4);
        else if (number.length < 4)
            throw new Error("Error Num！");
    }
    else if (gameType == "5D") {
        if (num.length != 5)
            throw new Error("Error Num!");
    }
    else throw new Error("Error Num！");
    return num;
}
var getPlayWayNumLen = function (playWay, num, companyNumLen) {
    if (playWay == "Head")
        return getHead(num, companyNumLen);
    else if (playWay == "Last")
        return getLast();
    else if (playWay == "HeadAndLast")
        return getHeadAndLast(num, companyNumLen);
    else if (playWay == "Roll")
        return getRoll(num, companyNumLen);
    else
        return;

}
var getHead = function (num, companyNumLen) {
    return findNumLen(num.toString().length, companyNumLen).Count;
}
var getLast = function () {
    return 1;
}
var getHeadAndLast = function (num, companyNumLen) {
    return getHead(num, companyNumLen) + getLast();
}
var getRoll = function (num, companyNumLen) {
    var len = 0;
    $(companyNumLen.NumLenList).each(function (i, obj) {
        if (obj.Length >= num.length)
            len += obj.Count;
    });
    return len;
}
var findNumLen = function (length, companyNumLen) {
    var numLen;
    $(companyNumLen.NumLenList).each(function (i, obj) {
        if (obj.Length == length) {
            numLen = obj;
            return false;
        }
    });
    return numLen;
}
//-------------------------End  金额计算-----------------------------------------------
